﻿using System.Collections.Generic;
using System.Linq;
using KLS.PPDS.Core.Entity;
using KLS.PPDS.Data.Database.Repository.EntityRepository;
using KLS.PPDS.Utilities;

namespace KLS.PPDS.ApplicationService.Manager
{
    public class RoleManager:RepositoryManager<RoleRepository>
    {
        public static Role Get(string roleName)
        {

            return Repository.FindAll().ToList().Where(c => c.RoleName.ToLower().Equals(roleName.ToLower())).FirstOrDefault();
        }


        public static Role GetRoleById(int roldId) 
        {
            return Repository.FindAll().Where(c => c.Id == roldId).FirstOrDefault();
        }

        public static bool IsSuperadminOrGraphicsRole(int roldId) 
        {
            var role = GetRoleById(roldId);

            if (role.RoleName == CommonConstant.Superadmin || role.RoleName == CommonConstant.Graphics) 
            {
                return true;
            }
            return false;
        }

        public static void Save(Role role )
        {
            var id = role.Id;
            role.InitAudit();
            Repository.SaveAndEvict(role);
            LogManager.LogDbAdd(role);

            if (id == 0)
            {
                LogManager.LogDbAdd(role);
            }
            else
            {
                LogManager.LogDbUpdate(role);
            }
        }

        public static void Update(Role role)
        {
            role.InitAudit();
            Repository.Update(role);
            LogManager.LogDbUpdate(role);

        }

        public static int CheckDuplicateDeletedRole(Role role, int companyId)
        {
          
            var validRole = Repository.FindAll().Where(u => u.RoleName == role.RoleName && u.IsDeleted == true).FirstOrDefault();
            if (validRole != null && validRole.Id > 0)
            {
            //    var userGroupReporitory = RepositoryManager<UserGroupRelRepository>.Repository();
            //    //var groupReporitory = RepositoryManager<GroupRepository>.Repository();
            //    var UserGroupByuser = userGroupReporitory.FindAll().Where(ug => ug.UserId == validUser.UserId).FirstOrDefault();
            //    //var Group = groupReporitory.FindAll().Where(g => g.CompanyId == companyId).FirstOrDefault();

            //    if (UserGroupByuser != null)
            //    {
            //        if (UserGroupByuser.Group.CompanyId == companyId)
            //        {
            //            validUser.Deleted = false;                        
            //            validUser.LanguageId = user.LanguageId;
            //            validUser.Password = user.Password;
            //            validUser.PhoneNo = user.PhoneNo;
            //            validUser.UpdatedBy = user.UpdatedBy;                        
            //            validUser.UpdatedDate = user.UpdatedDate;                        
            //            validUser.UserFullName = user.UserFullName;
            //            validUser.DepartmentId = user.DepartmentId;
            //            userReporitory.SaveAndEvict(validUser);

            //            return validUser.UserId;
            //        }
            //    }
            }
            return 0;
        }







        public static List<Role> GetAll()
        {
           
            return Repository.GetAll();
        }

        public static Role Get(int userid)
        {
          
            return Repository.FindAll().Where(c => c.Id.Equals(userid)).FirstOrDefault();
        }


        public static List<Role> GetRoles(int companyId)
        {
            
            //if (companyId > 0)
            //{
            //    var compayRepository = RepositoryManager<CompanyRepository>.Repository();
            //    var company = compayRepository.FindOne(companyId);
            //    //return UserRepository.FindAll().Where(u => u.Deleted == false).ToList().Where(x => x.Groups.Where(g => g.Group!=null && g.Group.CompanyId.Equals(companyId)).Count() > 0 || x.Groups.Count == 0).ToList();
            //    return UserRepository.FindAll().Where(u => u.Deleted == false && u.CompanyNo==company.CompanyNo).ToList();
            //}
            return Repository.FindAll().ToList();
        }

    }
  
}
